//
//  TJScreenBackgroundView.m
//  Unionpay
//
//  Created by admin on 2021/11/1.
//  Copyright © 2021 YAZX. All rights reserved.
//

#import "TJScreenBackgroundView.h"

@interface TJScreenBackgroundView()

@property (nonatomic, copy) closeBlock block;

@end

@implementation TJScreenBackgroundView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:[[UIScreen mainScreen] bounds]];
    if (self) {
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
        self.userInteractionEnabled = YES;
        self.closeTouchEnable = YES;
    }
    return self;
}

- (void)show:(closeBlock)closeBlock
{
    if (closeBlock) {
        self.block = closeBlock;
    }
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    self.alpha = 0.3;
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1;
    }];
}

- (void)close
{
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0.3;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
        if (self.block) {
            self.block(YES);
        }
    }];
}

# pragma mark 点击界面
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    if (self.closeTouchEnable) {
        [self close];
    }
}

- (void)dealloc
{
    NSLog(@"%@ 销毁", self);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
